#include "include/userinfo.h"


/************************************************/
/*       class:       user_list                 */
/*       function:    make_user_head_node       */
/************************************************/
void user_list::make_user_head_node ()
{
  if (pHead == NULL)
    {
      pHead = new user_node;
      strcpy (pHead->data.name,"null");
      strcpy (pHead->data.password ,"null");
      pHead->data.number = 9999999;
      pHead->pNext = NULL;
    }
}

/************************************************/
/*       class:       user_list                 */
/*       function:    make_user_node_from_file  */
/************************************************/
void user_list::make_user_node_from_file (user_node *user,FILE *fp,int inst_mthd)
{
  int retu = 0;
  user = pHead;
  user_node *new_node = NULL;//new point to the node
  while(1)
    {
      retu = make_user_new_node(&new_node,&fp);//ask for the new space to store the new node
      
      if(retu == END_OF_FILE)
	break; //at the end of reading the file, we break the programme and get out
      if(HEAD_INSERT == inst_mthd)
	head_insert(&user,new_node);
      else if(MIDD_INSERT == inst_mthd)
	midd_number_insert(&user,new_node);

    }
  return;
  
}


/*********************************************/
/*       class:       user_list              */
/*       function:    make_user_new_node     */
/*********************************************/
int user_list::make_user_new_node (user_node **new_node,FILE **fp)
{
  *new_node=new user_node ;//creat a new_node to the head of the point
  
  fscanf(*fp,"%s %s %d",(*new_node)->data.name, (*new_node)->data.password,&(*new_node)->data.number);
  
  if(feof(*fp) == END_OF_FILE)       //feof means returns a nonzero value if the end of the given file stream has been reached.
    {
      delete(*new_node);//free the memory
      goto lab0;
    }

  return 0;
 lab0: return END_OF_FILE;
  
}

// print the user infomation
void user_list::print_info(user_node* user)
{
  user_node *p = user->pNext;
     
  if( list_is_empty(user) == true) goto lab0;
  
  system("cls");	
  
  printf("\n   Number	  Name      Passeword\n");
  while(p != user)
    {
      printf("	%-8d%-8s%-9s\n",p->data.number,p->data.name,p->data.password);
      p = p->pNext ;
    }
  printf("\n");
  get_return("	That's all the information, press any button to go back");
  
 lab0: return;
  
}

// Add the infos at the head of the list
void user_list::head_insert(user_node** rootp , user_node* new_node)
{
  new_node->pNext = *rootp;
}


/*********************************************/
/*       class:       user_list              */
/*       function:    midd_number_insert     */
/*********************************************/
void user_list::midd_number_insert (user_node** rootp, user_node* new_node)
{
  user_node* current ;
  user_node* previous ;
  
  current  =  *rootp;
  previous = NULL;

  while ( current->data.number < new_node->data.number && current!=NULL )
    {
      previous = current;
      current = current->pNext;
    }
  
  if (previous ==  NULL)
    *rootp  =  new_node;
  else
    {
      previous->pNext  =  new_node;
      new_node->pNext  =  current;
    }
}

/*********************************************/
/*       class:       user_list              */
/*       function:    list_is_empty          */
/*********************************************/
int user_list::list_is_empty (user_node* list)
{
  if( NULL == list->pNext)
    {
      get_return ("List is empty, press any button to go back");
      return LIST_IS_EMPTY ;
    }
  return 0;
}



/*********************************************/
/*       class:       user_list              */
/*       function:    get_return             */
/*********************************************/
void user_list::get_return(char const *mess)
{
        printf("%s",mess);

        system("pause");

        return;
}



/*********************************************/
/*       class:       user_list              */
/*       function:    fopen_user_file        */
/*********************************************/
void user_list::fopen_user_file(FILE **fp,int mthd_of_fopen)
{
  if(__OR__ == mthd_of_fopen) *fp = fopen(USER_FILE_PATH,"r");	//only read
  if(__RW__ == mthd_of_fopen) *fp = fopen(USER_FILE_PATH,"r+");	//read and write
  if(__CLEAR_RW__ == mthd_of_fopen) *fp = fopen(USER_FILE_PATH,"w+");//only read, and the file will erase after the read
  if(NULL == *fp) 
    std::cout << " error for opening the file "<<std::endl;
  
  return;
  
}
